package edu.hrbeu.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.IService;
import edu.hrbeu.entity.Book;
import edu.hrbeu.entity.Discount;
import edu.hrbeu.mapper.BookMapper;
import edu.hrbeu.mapper.DiscountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * @author wzq
 * @create 2022-01-06-11:26
 */
@Service
public class BookService implements IService<Book> {

    @Autowired
    BookMapper bookMapper;

    @Autowired
    DiscountMapper discountMapper;

    @Override
    public BaseMapper<Book> getBaseMapper() {
        return bookMapper;
    }

    public List<Book> search(Book param) {

        QueryWrapper<Book> bookQueryWrapper = new QueryWrapper<Book>();
        if (param.getName() != null && !"".equals(param.getName())) {
            bookQueryWrapper.like("name", param.getName());
            System.out.println(1);
        }
        if (param.getCategory() != null&&param.getCategory().getId()!=0) {
            bookQueryWrapper.eq("category_id", param.getCategory().getId());
        }
        List<Book> bookList = bookMapper.selectList(bookQueryWrapper);

        QueryWrapper<Discount> queryWrapper
                = new QueryWrapper<Discount>()
                .eq("state", 1);
        List<Discount> discountList = discountMapper.selectList(queryWrapper);

        for (Discount discount : discountList) {
            long systemTimes = System.currentTimeMillis();
            if (discount.getDateLimit()) {
                long beginTime = discount.getBeginDate().getTime();
                if (systemTimes < beginTime) {
                    continue;
                }
                long endTime = discount.getEndDate().getTime() + 24 * 60 * 60 * 1000;
                if (systemTimes > endTime) {
                    continue;
                }
            }
            for (Book book : bookList) {
                if (book.getCategory().getId() == discount.getCategory().getId()) {
                    if (discount.getType().equals("全部")) {

                        float promotionPrice = book.getPrice() * discount.getRate();
                        book.setPromotionPrice((float) (Math.round(promotionPrice * 100) / 100));
                    }
                    if (discount.getType().equals("仅会员")) {
                        float vipPrice = book.getPrice() * discount.getRate();
                        book.setVipPrice((float) (Math.round(vipPrice * 100) / 100));
                    }
                }
            }
        }
        return bookList;
    }

    @Override
    public boolean saveBatch(Collection<Book> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdateBatch(Collection<Book> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean updateBatchById(Collection<Book> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdate(Book entity) {
        return false;
    }

    @Override
    public Book getOne(Wrapper<Book> queryWrapper, boolean throwEx) {
        return null;
    }

    @Override
    public Map<String, Object> getMap(Wrapper<Book> queryWrapper) {
        return null;
    }

    @Override
    public <V> V getObj(Wrapper<Book> queryWrapper, Function<? super Object, V> mapper) {
        return null;
    }


    @Override
    public Class<Book> getEntityClass() {
        return null;
    }

}
